Imports System
Imports System.Data
Imports System.Reflection
Imports System.Data.Common
Imports System.Collections.Generic

Public Enum ACEInstancia
   Consulta
   Nuevo
   Modificado
   Eliminado
End Enum

Public Class ACEsquemas

#Region " Campos "
   Private m_tipo As Type
#End Region

#Region " Constructores "
   Public Sub New(ByVal x_esquema As Object)
      m_tipo = x_esquema.GetType()
   End Sub
#End Region

#Region " Propiedades "
   Public Property ACTipoEsquema(ByVal x_esquema As Object) As Type
      Get
         Return m_tipo
      End Get
      Set(ByVal value As Type)
         m_tipo = x_esquema.GetType()
      End Set
   End Property
#End Region

#Region " Metodos "
   Public Sub ACCargarEsquemas(ByVal reader As DbDataReader, ByVal x_esquema As Object)
      Try
         Dim _campos As Integer = reader.FieldCount - 1
         For _index As Integer = 0 To _campos
            If Not reader.IsDBNull(_index) Then
               Dim _valor As Object = reader.GetValue(_index)
               Dim _campo As String = reader.GetName(_index)
               Dim _propertInfo As PropertyInfo
               _propertInfo = m_tipo.GetProperty(_campo)
               If Not (IsNothing(_propertInfo)) Then
                  Dim _tipoCampo As Type = _propertInfo.PropertyType ' tipo de la propiedad

                  'If (_tipoCampo.ToString() = "System.String") Then
                  '   _propertInfo.SetValue(x_esquema, _valor, Nothing)
                  'Else
                  '   _propertInfo.SetValue(x_esquema, Convert.ChangeType(_valor, _tipoCampo), Nothing)
                  'End If

                  ACSetValorEsquemas(_propertInfo, _tipoCampo.ToString(), _valor, x_esquema)

               End If
            End If
         Next
      Catch ex As Exception
         Throw ex
      End Try
   End Sub
   Public Sub ACCargarEsquemas(ByVal fila As DataRow, ByVal x_esquema As Object)
      Try
         Dim _campos As Integer = fila.Table.Columns.Count - 1
         For _index As Integer = 0 To _campos
            If Not IsDBNull(fila(_index)) Then
               Dim _valor As Object = fila(_index)
               Dim _campo As String = fila.Table.Columns(_index).ColumnName
               Dim _propertInfo As PropertyInfo
               _propertInfo = m_tipo.GetProperty(_campo)
               If Not (IsNothing(_propertInfo)) Then
                  Dim _tipoCampo As Type = _propertInfo.PropertyType ' tipo de la propiedad

                  'If (_tipoCampo.ToString() = "System.String") Then
                  '   _propertInfo.SetValue(x_esquema, _valor, Nothing)
                  'Else
                  '   _propertInfo.SetValue(x_esquema, Convert.ChangeType(_valor, _tipoCampo), Nothing)
                  'End If

                  ACSetValorEsquemas(_propertInfo, _tipoCampo.ToString(), _valor, x_esquema)

               End If
            End If
         Next
      Catch ex As Exception
         Throw ex
      End Try
   End Sub
   Private Function ACSetValorEsquemas(ByRef x_propertInfo As PropertyInfo, ByVal x_tipoCampo As String, ByVal x_valor As Object, ByRef x_esquema As Object) As Boolean
      Select Case x_tipoCampo
         Case "System.String"
            x_propertInfo.SetValue(x_esquema, x_valor, Nothing)
            Exit Select
         Case "System.Int16"
            If IsNumeric(x_valor) Then
               x_propertInfo.SetValue(x_esquema, Convert.ToInt16(x_valor), Nothing)
            End If
            Exit Select
         Case "System.Nullable`1[System.Int16]"
            If IsNumeric(x_valor) Then
               x_propertInfo.SetValue(x_esquema, Convert.ToInt16(x_valor), Nothing)
            End If
            Exit Select
         Case "System.Int32"
            If IsNumeric(x_valor) Then
               x_propertInfo.SetValue(x_esquema, Convert.ToInt32(x_valor), Nothing)
            End If
            Exit Select
         Case "System.Nullable`1[System.Int32]"
            If IsNumeric(x_valor) Then
               x_propertInfo.SetValue(x_esquema, Convert.ToInt32(x_valor), Nothing)
            End If
            Exit Select
         Case "System.Int64"
            If IsNumeric(x_valor) Then
               x_propertInfo.SetValue(x_esquema, Convert.ToInt64(x_valor), Nothing)
            End If
            Exit Select
         Case "System.Nullable`1[System.Int64]"
            If IsNumeric(x_valor) Then
               x_propertInfo.SetValue(x_esquema, Convert.ToInt64(x_valor), Nothing)
            End If
            Exit Select
         Case "System.Double"
            If IsNumeric(x_valor) Then
               x_propertInfo.SetValue(x_esquema, Convert.ToDouble(x_valor), Nothing)
            End If
            Exit Select
         Case "System.Nullable`1[System.Double]"
            If IsNumeric(x_valor) Then
               x_propertInfo.SetValue(x_esquema, Convert.ToDouble(x_valor), Nothing)
            End If
            Exit Select
         Case "System.Decimal"
            If IsNumeric(x_valor) Then
               x_propertInfo.SetValue(x_esquema, Convert.ToDecimal(x_valor), Nothing)
            End If
            Exit Select
         Case "System.Nullable`1[System.Decimal]"
            If IsNumeric(x_valor) Then
               x_propertInfo.SetValue(x_esquema, Convert.ToDecimal(x_valor), Nothing)
            End If
            Exit Select
         Case "System.DateTime"
            x_propertInfo.SetValue(x_esquema, Convert.ToDateTime(x_valor), Nothing)
            Exit Select
         Case "System.Nullable`1[System.DateTime]"
            If Not (x_valor = Nothing) Then
               x_propertInfo.SetValue(x_esquema, Convert.ToDateTime(x_valor), Nothing)
            End If
            Exit Select
         Case "System.Boolean"
            x_propertInfo.SetValue(x_esquema, Convert.ToBoolean(x_valor), Nothing)
            Exit Select
         Case "System.Nullable`1[System.Boolean]"
            x_propertInfo.SetValue(x_esquema, Convert.ToBoolean(x_valor), Nothing)
            Exit Select
      End Select
   End Function
#End Region

#Region " Metodos Compartidos "
   Public Shared Sub ACCargarEsquema(ByVal reader As DbDataReader, ByVal x_esquema As Object)
      Try
         Dim _tipo As Type
         _tipo = x_esquema.GetType()
         Dim _campos As Integer = reader.FieldCount - 1
         For _index As Integer = 0 To _campos
            If Not reader.IsDBNull(_index) Then
               Dim _valor As Object = reader.GetValue(_index)
               Dim _campo As String = reader.GetName(_index)
               Dim _propertInfo As PropertyInfo
               _propertInfo = _tipo.GetProperty(_campo)
               If Not (IsNothing(_propertInfo)) Then
                  Dim _tipoCampo As Type = _propertInfo.PropertyType ' tipo de la propiedad

                  'If (_tipoCampo.ToString() = "System.String") Then
                  '   _propertInfo.SetValue(x_esquema, _valor, Nothing)
                  'Else
                  '   _propertInfo.SetValue(x_esquema, Convert.ChangeType(_valor, _tipoCampo), Nothing)
                  'End If

                  ACSetValorEsquema(_propertInfo, _tipoCampo.ToString(), _valor, x_esquema)

               End If
            End If
         Next
      Catch ex As Exception
         Throw ex
      End Try
   End Sub
   Public Shared Sub ACCargarEsquema(ByVal fila As DataRow, ByVal x_esquema As Object)
      Try
         Dim _tipo As Type
         _tipo = x_esquema.GetType()
         Dim _campos As Integer = fila.Table.Columns.Count - 1
         For _index As Integer = 0 To _campos
            If Not IsDBNull(fila(_index)) Then
               Dim _valor As Object = fila(_index)
               Dim _campo As String = fila.Table.Columns(_index).ColumnName
               Dim _propertInfo As PropertyInfo
               _propertInfo = _tipo.GetProperty(_campo)
               If Not (IsNothing(_propertInfo)) Then
                  Dim _tipoCampo As Type = _propertInfo.PropertyType ' tipo de la propiedad

                  'If (_tipoCampo.ToString() = "System.String") Then
                  '   _propertInfo.SetValue(x_esquema, _valor, Nothing)
                  'Else
                  '   _propertInfo.SetValue(x_esquema, Convert.ChangeType(_valor, _tipoCampo), Nothing)
                  'End If

                  ACSetValorEsquema(_propertInfo, _tipoCampo.ToString(), _valor, x_esquema)

               End If
            End If
         Next
      Catch ex As Exception
         Throw ex
      End Try
   End Sub
   Private Shared Function ACSetValorEsquema(ByRef x_propertInfo As PropertyInfo, ByVal x_tipoCampo As String, ByVal x_valor As Object, ByRef x_esquema As Object) As Boolean
      Select Case x_tipoCampo
         Case "System.String"
            x_propertInfo.SetValue(x_esquema, x_valor, Nothing)
            Exit Select
         Case "System.Int16"
            If IsNumeric(x_valor) Then
               x_propertInfo.SetValue(x_esquema, Convert.ToInt16(x_valor), Nothing)
            End If
            Exit Select
         Case "System.Nullable`1[System.Int16]"
            If IsNumeric(x_valor) Then
               x_propertInfo.SetValue(x_esquema, Convert.ToInt16(x_valor), Nothing)
            End If
            Exit Select
         Case "System.Int32"
            If IsNumeric(x_valor) Then
               x_propertInfo.SetValue(x_esquema, Convert.ToInt32(x_valor), Nothing)
            End If
            Exit Select
         Case "System.Nullable`1[System.Int32]"
            If IsNumeric(x_valor) Then
               x_propertInfo.SetValue(x_esquema, Convert.ToInt32(x_valor), Nothing)
            End If
            Exit Select
         Case "System.Int64"
            If IsNumeric(x_valor) Then
               x_propertInfo.SetValue(x_esquema, Convert.ToInt64(x_valor), Nothing)
            End If
            Exit Select
         Case "System.Nullable`1[System.Int64]"
            If IsNumeric(x_valor) Then
               x_propertInfo.SetValue(x_esquema, Convert.ToInt64(x_valor), Nothing)
            End If
            Exit Select
         Case "System.Double"
            If IsNumeric(x_valor) Then
               x_propertInfo.SetValue(x_esquema, Convert.ToDouble(x_valor), Nothing)
            End If
            Exit Select
         Case "System.Nullable`1[System.Double]"
            If IsNumeric(x_valor) Then
               x_propertInfo.SetValue(x_esquema, Convert.ToDouble(x_valor), Nothing)
            End If
            Exit Select
         Case "System.Decimal"
            If IsNumeric(x_valor) Then
               x_propertInfo.SetValue(x_esquema, Convert.ToDecimal(x_valor), Nothing)
            End If
            Exit Select
         Case "System.Nullable`1[System.Decimal]"
            If IsNumeric(x_valor) Then
               x_propertInfo.SetValue(x_esquema, Convert.ToDecimal(x_valor), Nothing)
            End If
            Exit Select
         Case "System.DateTime"
            x_propertInfo.SetValue(x_esquema, Convert.ToDateTime(x_valor), Nothing)
            Exit Select
         Case "System.Nullable`1[System.DateTime]"
            If Not (x_valor = Nothing) Then
               x_propertInfo.SetValue(x_esquema, Convert.ToDateTime(x_valor), Nothing)
            End If
            Exit Select
         Case "System.Boolean"
            x_propertInfo.SetValue(x_esquema, Convert.ToBoolean(x_valor), Nothing)
            Exit Select
         Case "System.Nullable`1[System.Boolean]"
            x_propertInfo.SetValue(x_esquema, Convert.ToBoolean(x_valor), Nothing)
            Exit Select
      End Select
   End Function
#End Region

End Class